# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openvr
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.26.7
pkgrel=1
pkgdesc="API and SDK that allows access to VR hardware from multiple vendors (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/ValveSoftware/openvr/"
license=('spdx:BSD-3-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-python")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/ValveSoftware/openvr/archive/v${pkgver}.tar.gz
        002-install-headers.patch
        003-mingw-library-name.patch
        004-install-location.patch
        cxx_header_generator.py)
sha256sums=('e7391f1129db777b2754f5b017cfa356d7811a7bcaf57f09805b47c2e630a725'
            '10ece7ce59e78747c24ac98bb891d65ab6e57a888094a1fad5e87b56dff5ae10'
            '103ec4394f2fef820276906397011a498396694999e492c14ce0fda19f6d1014'
            '3fe7b4420a52ecf4233558ef5dbcc56f2abf35e204c90fbcefd13e0127a3938f'
            '739d0f4eeb8d83d2236d2174fbfac209ea20009bcaabba455c9b31301c14acfe')
noextract=(${_realname}-${pkgver}.tar.gz)

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  [[ -d ${srcdir}/${_realname}-${pkgver} ]] && rm -rf ${srcdir}/${_realname}-${pkgver}
  tar -xzf ${srcdir}/${_realname}-${pkgver}.tar.gz -C ${srcdir} || true

  cd "${srcdir}"/${_realname}-${pkgver}
  apply_patch_with_msg \
    002-install-headers.patch \
    003-mingw-library-name.patch \
    004-install-location.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d ${srcdir}/build-${MSYSTEM}-shared ]] && rm -r ${srcdir}/build-${MSYSTEM}-shared
  mkdir ${srcdir}/build-${MSYSTEM}-shared && cd ${srcdir}/build-${MSYSTEM}-shared

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED=ON \
    ${extra_config[@]} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build . #VERBOSE=1

  [[ -d ${srcdir}/build-${MSYSTEM}-static ]] && rm -r ${srcdir}/build-${MSYSTEM}-static
  mkdir ${srcdir}/build-${MSYSTEM}-static && cd ${srcdir}/build-${MSYSTEM}-static

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DBUILD_SHARED=OFF \
    ${extra_config[@]} \
    ../openvr-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build . #VERBOSE=1
}

package() {
  cd ${srcdir}/build-${MSYSTEM}-static
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  cd ${srcdir}/build-${MSYSTEM}-shared
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  ${MINGW_PREFIX}/bin/python ${srcdir}/cxx_header_generator.py -d "${pkgdir}${MINGW_PREFIX}/include/openvr"
  cp ${pkgdir}${MINGW_PREFIX}/include/openvr/openvr_mingw.hpp ${pkgdir}${MINGW_PREFIX}/include/openvr_mingw.hpp
  install -Dm644 "${srcdir}/openvr-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
